﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAFS05CHGD : DocumentBase
    {
        #region-------------Properties-----------------------
        DataRow LastRow = null;
        DataSet SetMap = null;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        YCAFS05CHGDData m_Data, m_DataX;
        public DataSet dsParamaters;
        YCAFS05CHGDReport m_rpt;
        DataTable tbUser = null; 

        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
     
        CollectionViewSource vwEKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS");
            }
        }

        CollectionViewSource vwFDECK
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFDECK");
            }
        }
        DataRow dwFDECK
        {
            get
            {
                if (vwFDECK.View == null)
                    return null;
                if ((DataRowView)vwFDECK.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFDECK.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWDECK
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwWDECK");
            }
        }
        DataRow dwWDECK
        {
            get
            {
                if (vwWDECK.View == null)
                    return null;
                if ((DataRowView)vwWDECK.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWDECK.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwFSSTUELI
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFSSTUELI");
            }
        }

        DataRow dwFSSTUELI
        {
            get
            {
                if (vwFSSTUELI.View == null)
                    return null;
                if ((DataRowView)vwFSSTUELI.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFSSTUELI.View.CurrentItem).Row;
            }
        }
 
        string ART
        {
            get
            {
                return m_Data.Left(txtMAT.Text.ToString() + "                      ", 20);
            }
        }

        string user = "";
        #endregion
        public YCAFS05CHGD()
        {
            InitializeComponent();
            this.Name = "删除原料";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = false;
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            tbUser = (DataTable)dicParamater["USERRIGHT"];
            m_Data = new YCAFS05CHGDData(OracleDB, proxy);
            user = (string)dicParamater["User"];
            txtMAT.Text = "BF4X023 ";
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {


            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }
        private void btnQuerySData_Click(object sender, EventArgs e)
        {
            try
            { 
              
                DataSet SResult = new DataSet();
                using (new LongOperation(this, "数据更新中"))
                {
                    SResult = m_Data.GetSData(ART);
                    SetProd.Clear();
                    SetProd.Merge(SResult);
                }
                if (vwFSSTUELI.View != null)
                {
                    vwFSSTUELI.View.MoveCurrentToFirst();
                }
                if (vwWDECK.View != null)
                {
                    vwWDECK.View.MoveCurrentToFirst();
                }
                if (vwFDECK.View != null)
                {
                    vwFDECK.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

            DataSet setRpt = new DataSet();

            if (setRpt == null)
                return;
            m_DataX = new YCAFS05CHGDData(dicParamater);
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            if (m_DataX.exAgent._app == null)
            {
                m_DataX.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
            }
            m_rpt = new YCAFS05CHGDReport(dicParamater);
            m_rpt.dsSource = SetProd;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("EXCEL");
        } 

        private void btnDeleteFS05_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwFSSTUELI == null)
                    return;
                string art = dwFSSTUELI["FSSART"].ToString();
                string mart = dwFSSTUELI["FSSMART"].ToString();
                string filter = "FSSART='" + art + "' and FSSMART='" + mart + "'";
                DataRow[] rows = SetProd.FSSTUELI.Select(filter);
                foreach (DataRow row in rows)
                {
                    string mandant = row["FSSMANDANT"].ToString();
                    string pos = row["FSSPOS"].ToString();
                    m_Data.INTEXDBC.DeleteFSSTUELI(mandant, art, pos, 1);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnRefreshAB_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string client, dart, krs, lfd, pos, upos, filter;
                DataRow[] rows;
                DataRow dwData;
                if (dwWDECK != null)
                {
                    client = dwWDECK["WDMANDANT"].ToString();
                    dart = dwWDECK["WDART"].ToString();
                    krs = dwWDECK["WDAUFKRS"].ToString();
                    lfd = dwWDECK["WDAUFLFD"].ToString();
                    pos = dwWDECK["WDAUFPOS"].ToString();
                    upos = dwWDECK["WDUPOS"].ToString();
                    filter = "WDAUFKRS='" + krs + "' and WDAUFLFD='" + lfd  + "' and WDART='" + dart + "'";
                    rows = SetProd.WDECK.Select(filter);
                    dwData = null;
                    foreach (DataRow row in rows)
                    {
                        pos = row["WDAUFPOS"].ToString();
                        upos = row["WDUPOS"].ToString();
                        m_Data.INTEXDBC.DeleteWDECK(client, krs, lfd, pos, upos, 3);
                        dwData = SetProd.ICC_ABAUFM.NewRow();
                        dwData["ABAMANDANT"] = row["WDMANDANT"];
                        dwData["ABAVKAUFKRS"] = row["WDAUFKRS"];
                        dwData["ABAVKAUFLFD"] = row["WDAUFLFD"];
                        dwData["ABAVKAUFPOS"] = row["WDAUFPOS"];
                        dwData["ABAVKUPOS"] = row["WDUPOS"];
                        m_Data.INTEXDBC.DeleteICC_ABAUFM(client, krs, lfd, pos, dwData, 4);
                    }
                }
                if (dwFDECK != null)
                {
                    client = dwFDECK["FDMANDANT"].ToString();
                    dart = dwFDECK["FDART"].ToString();
                    krs = dwFDECK["FDAUFKRS"].ToString();
                    lfd = dwFDECK["FDAUFLFD"].ToString();
                    pos = dwFDECK["FDAUFPOS"].ToString();
                    upos = dwFDECK["FDSETPOS"].ToString();
                    //FDAFOLGE,FDSETPOS
                    string step = dwFDECK["FDAFOLGE"].ToString();
                    filter = "FDAUFKRS='" + krs + "' and FDAUFLFD='" + lfd + "' and FDART='" + dart + "'";
                    rows = SetProd.FDECK.Select(filter);
                    foreach (DataRow row in rows)
                    {
                        pos = row["FDAUFPOS"].ToString();
                        upos = row["FDSETPOS"].ToString();
                        step = row["FDAFOLGE"].ToString();
                        m_Data.INTEXDBC.DeleteFDECK(client, krs, lfd, pos, step, upos, 3);
                        dwData = SetProd.ICC_ABAUFM.NewRow();
                        dwData["ABAMANDANT"] = row["FDMANDANT"];
                        dwData["ABAVKAUFKRS"] = row["FDAUFKRS"];
                        dwData["ABAVKAUFLFD"] = row["FDAUFLFD"];
                        dwData["ABAVKAUFPOS"] = row["FDAUFPOS"];
                        dwData["ABAART"] = row["FDART"];
                        dwData["ABAVAR"] =Helper.Right("   "+row["FDVAR"].ToString(),3);
                        dwData["ABAFARBE"] = row["FDFARBE"];
                        dwData["ABAGROE"] = row["FDGROE"];
                        DataSet SResult = m_Data.INTEXDBC.QueryICC_ABAUFM(dwData, 16);
                        foreach (DataRow rx in SResult.Tables[0].Rows)
                        {
                            m_Data.INTEXDBC.DeleteICC_ABAUFM(client, krs, lfd, pos, rx, 4);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
      
    }
}
